<?

function getDateTimesFromRequest($post_request) {
  $start_time_string = trim($post_request['start_date'])." ".trim($post_request['start_time']);
  $end_time_string = trim($post_request['end_date'])." ".trim($post_request['end_time']);
  
  $start_time_period = $post_request['start_time_period'];
  $end_time_period = $post_request['end_time_period'];
  
  $start_time = new DateTime($start_time_string);
  $end_time = new DateTime($end_time_string);
  
  // date and time adjustments
  // "add" and "sub" only work with PHP >=5.3, can't use for now
  if ($start_time->format('g') == '12') {
    //if ($start_time_period == 0) $start_time->sub(new DateInterval('PT12H'));
    if ($start_time_period == 0) {
      $start_time->setTime((int)$start_time->format('H') - 12, 
                          (int)$start_time->format('i'));
    }
  }
  else {
    //if ($start_time_period == 1) $start_time->add(new DateInterval('PT12H'));
    if ($start_time_period == 1) {
      $start_time->setTime((int)$start_time->format('H') + 12, 
                           (int)$start_time->format('i'));
    }
  }
  
  if ($end_time->format('g') == '12') {
    //if ($end_time_period == 0) $end_time->sub(new DateInterval('PT12H'));
    if ($end_time_period == 0) {
      $end_time->setTime((int)$end_time->format('H') - 12, 
                         (int)$end_time->format('i'));
    }
  }
  else {
    //if ($end_time_period == 1) $end_time->add(new DateInterval('PT12H'));
    if ($end_time_period == 1) {
      $end_time->setTime((int)$end_time->format('H') + 12, 
                         (int)$end_time->format('i'));
    }
  }
  //if ((int)$start_time->format('Y') < 100) $start_time->add(new DateInterval('P2000Y'));
  if ((int)$start_time->format('Y') < 100) {
    $start_time->setDate((int)$start_time->format('Y') + 2000, 
                         (int)$start_time->format('m'), 
                         (int)$start_time->format('d'));
  }
  //if ((int)$end_time->format('Y') < 100) $end_time->add(new DateInterval('P2000Y'));
  if ((int)$end_time->format('Y') < 100) {
    $end_time->setDate((int)$end_time->format('Y') + 2000, 
                       (int)$end_time->format('m'), 
                       (int)$end_time->format('d'));
  }
  
  return array($start_time, $end_time);
}

?>
